<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadController extends Controller
{
    /**
     * 上传图片
     * @return array
     */
    public function upload(Request $request){
        $code = 0;
        $data = [];
        if ($request->method()== 'POST') {
            $path = $request->file('file')->store(date('ymd'),'upload');
            if ($path){
                $fileUrl =asset('/uploadImages/' . $path);
                $code = 1;
                $data = $fileUrl;
                $message = '上传成功';
            }else{
                $message = "上传失败";
            }
        } else {
            $message = "参数错误";
        }
        return $this->showMsg($code, $message,$data);
    }
    public function showMsg($code,$message = '',$data = array()){
        $result = array(
            "success" => $code,
            "message" =>$message,
            "url" =>$data
        );
        exit(json_encode($result));
    }
}
